﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Microsoft.Phone.Controls;

namespace Win7RovMobileApp
{
    public partial class VehiclesMenu : PhoneApplicationPage
    {
        private String[] ImageSources = { "../Images/auto.png", 
                                         "../Images/pickup.png",
                                         "../Images/van.png",
                                         "../Images/bus.png",
                                         "../Images/smallTruck.png",
                                         "../Images/truck.png" };

        private String[] VehicleNames = { "Autoturism", 
                                         "Transport marfa 0 - 3,5 t",
                                         "Transport marfa 3,5 - 7,5 t",
                                         "Transport marfa 7,5 - 12,0 t",
                                         "Transport marfa > 12 t (maxim 3 axe)",
                                         "Transport marfa > 12 t (maxim 4 axe)" };

        private double ImageMargin      = 1;		// Margin between Images
        private double ImageWidth       = 140;	    // Image width
        private double ImageHeight      = 90;       // Image height
        private double ImageResizeScale = 2;	    // Resize scale (when focused) 
        private int    SelectedVehicle  = 0;        // Index counter for car images


        public VehiclesMenu()
        {
            InitializeComponent();
            Height = 768; Width = 480;
            AddImages();
        }


        private void AddImages()
        {
            for (int i = 0; i < ImageSources.Length; i++)
            {
                //create validPeriod
                Vehicle vehicle = new Vehicle(ImageSources[i], VehicleNames[i]);

                // resize the image
                ResizeImage(vehicle, ImageWidth, ImageHeight, i, ImageSources.Length);

                //add to view
                MenuPanel.Children.Add(vehicle);

                //add event handler for hovering upon it
                vehicle.MouseEnter += new MouseEventHandler(delegate(object ob, MouseEventArgs e)
                {
                    //no matter what, at first, all images MUST be normal scaled
                    for (int k = 0; k < MenuPanel.Children.Count; k++)
                    {
                        Vehicle vehicleImg = MenuPanel.Children[k] as Vehicle;
                        ResizeImage(vehicleImg, ImageWidth, ImageHeight, k, ImageSources.Length);

                        if (vehicleImg == (ob as Vehicle))
                        {
                            SelectedVehicle = k;
                        }
                    }

                    // resize the hovered imag
                    SetImageCapture(SelectedVehicle, ob);
                });


                //when double tap an image, go to next menu screen 
                vehicle.DoubleTap += new EventHandler<System.Windows.Input.GestureEventArgs>(delegate(object o, System.Windows.Input.GestureEventArgs e)
                {
                    App.UserPreferences.VehicleType = VehicleNames[SelectedVehicle];
                    NavigationService.Navigate(new Uri("/Menus/ValabilityMenu.xaml", UriKind.Relative));
                });

            }

            //set by default photo 1 as the one highlighted
            SetImageCapture(0, MenuPanel.Children[0]);
        }


        private void SetImageCapture(int i, object ob)
        {
            ResizeImage(ob as Vehicle, ImageWidth * ImageResizeScale, ImageHeight * ImageResizeScale, i, ImageSources.Length);
            (ob as Vehicle).Description.Visibility = Visibility.Visible;
            (ob as Vehicle).Photo.Opacity = 1;
        }


        // resize the image
        private void ResizeImage(Vehicle vehicleImage, double imageWidth, double imageHeight, int index, int total)
        {
            vehicleImage.Description.Visibility = Visibility.Collapsed;

            vehicleImage.Photo.Width = imageWidth;
            vehicleImage.Photo.Height = imageHeight;
            vehicleImage.Photo.Opacity = 0.7;

            vehicleImage.SetValue(Canvas.TopProperty, Height - vehicleImage.Photo.Height / 2);
            vehicleImage.SetValue(Canvas.LeftProperty, Width / 2 + (index - (total - 1) / 2) * (ImageMargin + ImageWidth) - vehicleImage.Photo.Width / 2);
        }
    }
}